package com.xht.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.xht.domain.ResponseResult;
import com.xht.domain.dto.CategoryChangeStatusDto;
import com.xht.domain.dto.CategoryDto;
import com.xht.domain.entity.Category;
import com.xht.enums.AppHttpCodeEnum;
import com.xht.service.CategoryService;
import com.xht.utils.BeanCopyUtils;
import com.xht.utils.WebUtils;
import com.xht.vo.ExcelCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/content/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("listAllCategory")
    public ResponseResult listAllCategory() {
        return categoryService.listAllCategory();
    }

    /**
     * 分类模块 - 导出数据到excel
     */
    @PreAuthorize("@ps.hasPermission('content:category:export')") //添加查询前的权限判断,hasPermission方法返回boolean类型
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        try {
            //设置下载文件的请求头
            WebUtils.setDownLoadHeader("分类.xlsx", response);
            //获取需要导出的数据
            List<Category> categoryVos = categoryService.list();
            List<ExcelCategoryVo> excelCategoryVos =
                    BeanCopyUtils.copyBeanList(categoryVos, ExcelCategoryVo.class);
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(),
                            ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryVos);
        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result =
                    ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }

    /**
     * 分类列表 -- 分页查询和模糊查询
     */
    @GetMapping("/list")
    public ResponseResult list(Integer pageNum, Integer pageSize, String name, String status) {
        return categoryService.categoryList(pageNum, pageSize, name, status);
    }

    /**
     * 新增分类
     */
    @PostMapping
    public ResponseResult addCategory(@RequestBody CategoryDto categoryDto) {
        return categoryService.addCategory(categoryDto);
    }

    /**
     * 编辑 所选分类--回显
     */
    @GetMapping("/{id}")
    public ResponseResult getCategoryById(@PathVariable Integer id) {

        return categoryService.getCategoryById(id);
    }

    /**
     * 编辑 所选分类--更新category表
     */
    @PutMapping
    public ResponseResult updateCategory(@RequestBody Category category) {
        return categoryService.updateCategory(category);
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteCategory(@PathVariable Integer id) {
        return categoryService.deleteCategory(id);
    }

    /**
     * 更改分类状态
     */
    @PutMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody CategoryChangeStatusDto categoryChangeStatusDto) {
        return categoryService.changeStatus(categoryChangeStatusDto);
    }
}
